nehnre.loading = false;
$(function(){
	loadUserInfo();
	$("#chat_panel").keydown(sendMessage).focus(function(){
		if($(this).html() == "点此提交你的问题"){
			$(this).html("");
		}
	}).blur(function(){
		if(!$(this).html()){
			$(this).html("点此提交你的问题");
		}
	});
	$("#btn_send").click(function(){
		sendMessage.call($("#chat_panel").get(0),{which:13});
	});
	showInPanel("系统", "欢迎您使用研发平台智能问答系统...");
});

/**
 * 发送消息
 */
function sendMessage(e){
	var reg = /<[^>]*>/ig;
	var chatContent = $(this).html();
	if($.trim(chatContent)){
		if($.trim(chatContent) != "点此提交你的问题"){
			chatContent = $.trim(chatContent);
			chatContent = chatContent.replace(reg,"");
			if(chatContent && e.which == 13){
				if(chatContent.replace(/&[^;]*;/ig,"").length > 150){
					alert("提交字数不能超过150字！");
					return false;
				}
				if(nehnre.loading){
					alert("你正在发送请求中，请不要重复发送...");
					return false;
				}
				//提交数据到服务器
				sendDataToServer(chatContent);
				//显示在show_panel里面
				showInPanel("用户", chatContent);
				$(this).html("");
				$("#btn_send").focus();
				$(this).focus();
			}
		}
		if(e.which == 13){
			return false;
		}
	}
}

/**
 * 提交到后台
 */	
function sendDataToServer(content){
	if($.trim(content)){
		content = nehnre.encode($.trim(content));
		nehnre.loading = true;
		$.ajax({
			url:"receive.jsp",
			type: "POST",
			cache:false,
			data: "content=" + content,
			success: function(returnValue){
				returnValue = nehnre.parseJSON(returnValue);
				var result = returnValue[0];
				var keys = orderKeys(returnValue[1]);
				if(result.length>0){
					showQuestionAnswer(result, keys);
				} else {
					showInPanel("系统", "非常抱歉，未找到你问题的答案");
				}
			},
			error: function(err){
			},
			complete: function(){
				nehnre.loading = false;
			}
		});
	}
}

/**
 * 在显示面板中显示
 */	
function showInPanel(username, content, color){
	color = color?color:"yellow";
	var userInfo = "<div><font color=\"" + color + "\">" + username + "(" + new Date().format("yyyy-MM-dd hh:mm:ss") + "):</font></div>";
	$("#show_panel").append("<div>" + userInfo + content + "</div>");
	$("#show_panel").get(0).scrollTop=$("#show_panel").get(0).scrollHeight;
}
function showQuestionAnswer(result, keys){
	var content;
	var baseMark;
	var chlen = 25;
	var height = $("#show_panel").get(0).scrollHeight;
	for(var i=0;i<result.length;i++){
		if(!i){
			baseMark = result[i].count;
			content = "<div><font color=\"yellow\">系统(" + new Date().format("yyyy-MM-dd hh:mm:ss") + ")共检索出"+result.length+"条:</font></div>";
		} else {
			content = "<hr>";
		}
		var star = Math.round(result[i].count*5/baseMark);
		if(!star){
			star = 1;
		}
		content += "<div onmouseover='$(this).css(\"background-color\",\"black\")' onmouseout='$(this).css(\"background-color\",\"\")'>";
		content += "<div>" + (i + 1) + "、问：" + handleAnswer(result[i].question, keys, true);
		for(var j=0;j<star;j++){
			content += "★";
		}
		content += "</div>";
		
		if(result[i].answer.length > chlen){
			content += "<div>答：" + handleAnswer(result[i].answer.substring(0,chlen), keys, true);
			content += "<a href='javascript:;' onclick='$(this).next().show();$(this).hide();'>全部展开↓</a><span style='display:none'>"+handleAnswer(result[i].answer.substring(chlen),keys)+"</span></div>";
		} else {
			content += "<div>答：" + handleAnswer(result[i].answer,keys) + "</div>";
		}
		content += "</div>";
		$("#show_panel").append(content);
	}
	if(height > 256){
		//$("#show_panel").get(0).scrollTop=height - 40;
		$("#show_panel").animate({scrollTop:height - 40},1000);
	}
}
function orderKeys(keys){
	var resultKey = [];
	for(o in keys){
		resultKey.push(o);
	}
	return removeSameLastChar(resultKey);
}
function maxKey(keys){
	var maxk = "";
	for(o in keys){
		if(o.length > maxk.length){
			maxk = o;
		}
	}
	return maxk;
}
function removeSameLastChar(keys){
	var len_key = keys.length;
	var rv = [];
	for(var i=0;i<len_key;i++){
		if(keys[i-1] && keys[i-1].substring(keys[i-1].length-1) == keys[i].substring(0,1)){
			delete keys[i-1];
		}
	}
	for(o in keys){
		rv.push(keys[o]);
	}
	return rv;
}
function handleAnswer(answer, keys, title){
	if(!title){
		answer = answer.replace(/\n/ig,"<br>");
	}
	for(var i=0;i<keys.length;i++){
		answer = answer.replace(new RegExp(keys[i],"ig"),"<font color='yellow'>" + keys[i] + "</font>");
	}
	return answer;
}
function loadUserInfo(){
	$.getJSON("http://member.sgst.cn/jsp/user_info.jsp?jsoncallback=?", function(result){
		if(result.success){
			$(".STYLE2").eq(0).html(nehnre.decode(result.data.logName));
			$(".STYLE2").eq(1).html(nehnre.decode(result.data.trueName));
		} else if(result.errorType==1){
			$(".STYLE2").eq(0).html("<a href='http://member.sgst.cn/jump.do?backurl="+nehnre.encode(location.href)+"'><font color='red'>未登录或登录超时！</font></a>");
		}
	});	
}